/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evidence.view.pomocneTridy;

import evidence.model.pomocneTridy.Time;
import evidence.model.pomocneTridy.TimeException;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Protheo
 */
public final class DateSpinnerModel extends SpinnerDateModel implements Serializable {

    public static final long serialVersionUID = 1;

    @SuppressWarnings("deprecation")
    public DateSpinnerModel() {
        super(new Date(), null, null, Calendar.MINUTE);
        this.setValue(new Date());
    }

    @Override
    public int getCalendarField() {
        return super.getCalendarField();
    }

    @Override
    public Date getDate() {
        return super.getDate();
    }

    @Override
    public Comparable getEnd() {
        return super.getEnd();
    }

    @Override
    public Comparable getStart() {
        return super.getStart();
    }

    @Override
    public void setCalendarField(int calendarField) {
        super.setCalendarField(Calendar.MINUTE);
    }

    @Override
    public void setEnd(Comparable end) {
        super.setEnd(end);
    }

    @Override
    public void setStart(Comparable start) {
        super.setStart(start);
    }

    @Override
    public Object getValue() {
        return super.getValue();
    }

    @Override
    @SuppressWarnings("deprecation")
    public void setValue(Object value) {
        Date dateValue = (Date) value;
        int minute = dateValue.getMinutes();
        if (minute!=15 || minute!=30 || minute!=45 || minute!=0) {
            if (dateValue.getMinutes() >= 0 && dateValue.getMinutes() <= 14) {
                dateValue.setMinutes(0);
            } else if (dateValue.getMinutes() >= 15 && dateValue.getMinutes() <= 29) {
                dateValue.setMinutes(15);
            } else if (dateValue.getMinutes() >= 30 && dateValue.getMinutes() <= 44) {
                dateValue.setMinutes(30);
            } else if (dateValue.getMinutes() >= 45 && dateValue.getMinutes() <= 59) {
                dateValue.setMinutes(45);
            }
        }
        super.setValue(dateValue);
    }

    @SuppressWarnings("deprecation")
    @Override
    public Object getNextValue() {
        Date value = (Date) super.getValue();
            if (value.getMinutes() == 0) {
                value.setMinutes(15);
            } else if (value.getMinutes() == 15) {
                value.setMinutes(30);
            } else if (value.getMinutes() == 30) {
                value.setMinutes(45);
            } else if (value.getMinutes() == 45) {
                value.setMinutes(0);
                value.setHours(value.getHours()+1);
            }
        return value;
    }

    @SuppressWarnings("deprecation")
    @Override
    public Object getPreviousValue() {
        Date value = (Date) super.getValue();
            if (value.getMinutes() == 15) {
                value.setMinutes(0);
            } else if (value.getMinutes() == 30) {
                value.setMinutes(15);
            } else if (value.getMinutes() == 45) {
                value.setMinutes(30);
            } else if (value.getMinutes() == 0) {
                value.setMinutes(45);
                value.setHours(value.getHours()-1);
            }
        return value;
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        super.addChangeListener(l);
    }

    @Override
    public void removeChangeListener(ChangeListener l) {
        super.removeChangeListener(l);
    }
}
